<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 用户模型
 *
 * @author www.dingwei.cn
 */

class AdmSettingModel extends MY_Model{

	private $tbSetting = 'admin_config';
    public $tbDlnSetting = 'admin_deliveryman_config';
	private $tbLocation = 'admin_location';
    public $tbExplain = 'admin_explain';
    public $tbShare = 'admin_share';
    public $tbBank = 'admin_bank';
	/**
	 * 获取配置信息
	 * @param 	string $key
	 * @param 	string $port = admin|deliveryman
	 * @return	mixed
	 */
	public function get($key = null, $port = 'admin'){

		if($key){
			$this->db->select($key);

			if($port == 'admin'){
			     $tA = $this->getOne($this->tbSetting);
			}else{
			    $tA = $this->getOne($this->tbDlnSetting);
			}

			if( ! strpos($key, ',')){
				return isset($tA[$key]) ? $tA[$key] : '';
			}
			
			return $tA;
		}

		if($port == 'admin'){
            return $this->getOne($this->tbSetting);
        }

        return $this->getOne($this->tbDlnSetting);
	}

	/**
	 * 获取配置信息
	 * @param 	string $key
	 * @return	mixed
	 */
	public function getExplain($key = null){

		if($key){
			$this->db->select($key);

            $tA = $this->getOne($this->tbExplain);

			if( ! strpos($key, ',')){
				return isset($tA[$key]) ? $tA[$key] : '';
			}

			return $tA;
		}

        return $this->getOne($this->tbExplain);
	}
	
	/**
	 * 保存数据
	 * @param array $data
	 * @return boolean
	 */
	public function save($data){
        return $this->update($this->tbSetting, $data);
	}

    /**
     * 获取所有的省份
     *
     * @return	array
     */
    public function getProvinces($id = null){

        $this->db->select('id,name')->where('parent_id', 1)
            ->order_by('sort asc,id asc');
        if($id){
            $this->db->where_in('id', $id);
        }
        return $this->getAll($this->tbLocation);
    }

    /**
     * 根据省份id获取城市列表
     * @param	integer	$id
     * @return	array
     */
    public function getCitiesByProvinceId($id){

        if( ! $id){
            return array();
        }

        $this->db->select('id,name')
            ->where('pid', $id)
            ->order_by('sort asc,id asc');
        return $this->getAll($this->tbLocation);
    }

    /**
     * 根据城市id获取区县列表
     * @param	integer	$id
     * @return	array
     */
    public function getCountyByCityId($id){

        if( ! $id){
            return array();
        }

        $this->db->select('id,name')
            ->where('pid', $id)
            ->order_by('sort asc,id asc');
        return $this->getAll($this->tbLocation);
    }

    /**
     * 获取银行列表
     * @return	array
     */
    public function getBanks(){

        $this->db->select('id,bank_name as name,bank_icon as icon')->order_by('id desc');
        $data = $this->getAll($this->tbBank);
        foreach ($data as $k => $v){
            $data[$k]['icon'] = base_url($v['icon']);
        }

        return $data;
    }

    /**
     * 验证银行是否存在
     * @param integer $id
     * @return	boolean
     */
    public function existBank($id){

        $this->db->where('id', $id);
        if($this->count($this->tbBank)){
            return true;
        }

        return false;
    }

    /**
     * 获取所有的银行卡
     * @param 	integer $id
     * @return	array
     */
    public function getBankByIds($id){

        $this->db->select('id,bank_name as name,bank_icon as icon')
            ->where_in('id', $id);
        $tA = $this->getAll($this->tbBank);
        $data = array();
        foreach ($tA as $v){
            $data[$v['id']] = $v;
            $data[$v['id']]['icon'] = base_url($v['icon']);
        }
        return $data;
    }
/***********************************************************************************************************************
 *  区域
***********************************************************************************************************************/

    /**
	 * 单个获取
	 */
	public function getLocation(){
		//$this->db->limit(400);
        $this->db->order_by('sort asc,id asc');
		$tA = $this->getAll($this->tbLocation);
		$tA = getchild(0, $tA ,'id', 'parent_id');
		return $tA;
	}

    /**
     * 获取某级区域
     * @param $parentId   父id
     * @return array
     */
    public function getLocationByPid($parentId, $whereIn = []){
//         if(!$parentId) return array();
        $this->db->select('id,name')
            ->order_by('sort asc,id asc')
            ->where_in('parent_id', $parentId);
        if($whereIn){
            $this->db->where_in('id', $whereIn);
        }
        $tA = $this->getAll($this->tbLocation);
        return $tA;
    }

    /**
     * 获取多个区域名称
     * @param $locationId  区域id
     * @return array
     * @ahthor khj
     */
	public function getLocationNameByIds($locationId){

	    if(!$locationId) return array();

	    if(! is_array($locationId)) return array();

	    $this->db->select('id,name')
                ->order_by('sort asc,id asc')
                 ->where_in('id', $locationId);

        $tA = $this->getAll($this->tbLocation);
        $data = array();
        foreach ($tA as $k => $v){
            $data[$v['id']] = $v;
        }
        return $data;
    }
}